package com.jt.www.biz.user_wxb.controller;

import com.jt.www.biz.user_wxb.service.LoginService;
import com.jt.www.domain.enums.wxb.LoginTypeEnum;
import com.jt.www.domain.vo.LoginVO;
import com.jt.www.enums.ResultEnum;
import com.jt.www.model.reps.ResultDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author shaoyn
 * @create 2020年2月25日
 */
@Api(value = "我学保登录", tags = {"LOGIN"}, description = "我学保登录")
@RestController
@RequestMapping(value = "/wxb")
public class LoginController {

    public static final Logger log = LoggerFactory.getLogger(LoginController.class);
    @Autowired
    private LoginService loginService;

    /**
     * 更新支付状态
     *
     * @param loginVO
     * @return
     */
    @ApiOperation(value = "登录与注册", notes = "登录与注册", httpMethod = "POST")
    @PostMapping(value = "/login")
    public ResultDTO login(@RequestBody LoginVO loginVO) {

        if (loginVO == null || StringUtils.isBlank(loginVO.getLoginType())) {
            return new ResultDTO(ResultEnum.PARAM_ERROR, "登录类型不能为空!");
        }

        // 如果是密码登录 查询用户名
        if (loginVO.getLoginType().equals(LoginTypeEnum.PASSWORD.getCode())
                && StringUtils.isBlank(loginVO.getUserName())
                || loginVO.getLoginType().equals(LoginTypeEnum.KAPTCHA_CODE.getCode())
                && StringUtils.isBlank(loginVO.getPhoneNum())) {
            return new ResultDTO(ResultEnum.PARAM_ERROR, "手机号不能为空!");
        }

        try {
            return loginService.registerAndLogin(loginVO);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultDTO(ResultEnum.SERVER_ERROR, "请求失败");
        }
    }

    /**
     * 是否第一次登录
     *
     * @param userCode
     * @return
     */
    @ApiOperation(value = "是否第一次登录", notes = "是否第一次登录", httpMethod = "GET")
    @GetMapping(value = "/isFirstLogin")
    public ResultDTO isFirstLogin(@RequestParam("userCode") String userCode) {

        if (StringUtils.isBlank(userCode)) {
            //用户的usercode 不能为空
            return new ResultDTO(ResultEnum.ERROR_9999, "登录异常!");
        }
        try {
            return loginService.isFirstLogin(userCode);
        } catch (Exception e) {
            return new ResultDTO(ResultEnum.ERROR_9999, "请求失败!");
        }
    }


}
